<?php
$nowid = $_POST['jz_id'];
$allhtml = $_POST['jz_ancon'];
$ansan = $_POST['jz_ansan'];
$justbill = $_POST['jz_zb'];
$fromip = $_POST['jz_ip'];
$name = $_POST['jz_name'];
$jz_authid = $_POST['jz_authid'];
$userid = '';
function gennerateuserid($conn, $length = 7) {
    $characters = 'abcdefghijklmnopqrst1456789';
    $id = '';
    do {
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $characters[rand(0, strlen($characters) - 1)];
        }
        $result = $conn->query("SELECT 1 FROM exam_ans WHERE lisid = '$id'");
        if ($result !== false && $result->num_rows > 0) {
            $id = ''; 
        } else {
            break;
        }
    } while ($id === '');
    return $id;
}
require('../../cofd/common.php');
$mysqli = $conn;
$userid = gennerateuserid($conn, 15);
$ansan_json = preg_replace('/\s+/', '', $ansan);
$stmt = $mysqli->prepare("INSERT INTO exam_ans (fromexam, anscon, ansan,ifzb,lisid,ip,aname,bindid) VALUES (?, ?, ?, ?, ?,?,?,?)");
$stmt->bind_param('sssissss', $nowid,$allhtml, $ansan_json,$justbill, $userid,$fromip,$name,$jz_authid); 
if($stmt->execute()){
    echo json_encode(array('status' => 200,'id'=>$userid));
}else{
    echo json_encode(array('status' => 500));
}
$stmt->close();
$mysqli->close();